package cate.game.pvp.ladder.old;

import cate.common.table.d.GDOperation;
import cate.game.client.msg.ClientMsg;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarm;
import cate.game.role.farm.PlayTimes;
import cate.game.role.fight.FuncPosition;
import cate.game.role.fight.Position;
import cate.game.role.pvp.PvpRecord;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

import static cate.common.table.d.GDFunc.ADVENTURE;
import static cate.common.table.d.GDFunc.LADDER_RACE;

@ClientMsg(title = "玩家天梯数据")
public class LadderOld extends AbstractFarm {

    @NoteField("当前赛季")
    public int season;

    @NoteField("挑战次数")
    public PlayTimes playTimes;

    @NoteField("扫荡次数")
    public PlayTimes mopTimes;

    @NoteField("当前排名")
    public int rank;

    @NoteField("对手列表")
    @JsonIgnore
    public List<LadderOpponentOld> opponents;

    @NoteField("最近刷新时间")
    @JsonIgnore
    public long recentRefreshTime;

    @NoteField("玩家录像")
    @JsonIgnore
    public LinkedList<PvpRecord> records;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (Objects.isNull(playTimes)) {
            playTimes = new PlayTimes();
        }
        playTimes.initialize();

        if (Objects.isNull(mopTimes)) {
            mopTimes = new PlayTimes();
        }
        mopTimes.initialize();

        if (Objects.isNull(opponents)) {
            opponents = Lists.newLinkedList();
        }

        if (Objects.isNull(records)) {
            records = Lists.newLinkedList();
        }
    }

    @Override
    public void onEnter() {
        super.onEnter();
        checkSeason();
    }

    private void checkSeason() {
        //
    }

    private void clearData() {
        this.playTimes.resetAll();
        this.mopTimes.resetAll();
        this.records.clear();
        this.opponents.clear();
        this.recentRefreshTime = 0L;
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        this.playTimes.resetAll();
        this.mopTimes.resetAll();
        if (!silence) {
            noticeUpdate();
        }
    }

    @Override
    public void onAfterOpen() {
        checkPosition();
        noticeUpdate();
    }

    private void checkPosition() {
        if (!open) {
            return;
        }
        if (role.getFight().funcPositions.get(LADDER_RACE) == null
                || role.getFight().funcPositions.get(LADDER_RACE).positions.isEmpty()) {
            Position position = role.getFight().getPosition(ADVENTURE);
            if (position != null) {
                FuncPosition funcPosition = new FuncPosition();
                funcPosition.init();
                funcPosition.funcId = LADDER_RACE;
                funcPosition.positions.add(position.copy());
                role.getGame().fight.position.updatePosition(role, funcPosition);
            }
        }
    }

    @Override
    public int getFightFuncId() {
        return LADDER_RACE;
    }

    @Override
    public int getFuncId() {
        return LADDER_RACE;
    }

    public EcResult<Void> mop() {
        EcResult<Void> r = new EcResult<>();
        if (!canChallenge()) {
            r.fail("结算期间无法挑战");
        }

        r = this.playTimes.available(role, getFuncId());

        if (!r.ok()) {
            return r;
        }

        if (CollectionUtils.isEmpty(role.getGame().table.ladder.base.getList())) {
            return r.fail("配置错误!");
        }

        String mopRwdStr = role.getGame().table.ladder.base.getList().get(0)
                .mopRwdStr;
        if (!StringUtils.hasLength(mopRwdStr)) {
            return r.fail("配置错误!");
        }

        this.playTimes.added();

        MixRes reward = new MixRes(mopRwdStr);
        role.log.setOperation(GDOperation.LADDER_MOP_RWD);
        reward.add(role, true, GDOperation.LADDER_MOP_RWD);
        role.log.clearOperation();

        role.getGame().notice.dialogReward(role, reward);

        noticeUpdate();
        role.getHistory().action.ladderChallange();
        return r.success();
    }

    public EcResult<Void> challengeCheck(String opponentIdx) {
        EcResult<Void> r = new EcResult<>();
        if (!isOpen()) {
            return r.fail("功能尚未开启");
        }
        if (isFighting()) {
            return r.fail("当前正在战斗中");
        }

        if (!canChallenge()) {
            return r.fail("结算期间无法挑战");
        }

        if (!StringUtils.hasLength(opponentIdx)) {
            return r.fail("对手ID参数错误!");
        }

        if (opponentIdx.equalsIgnoreCase(role.getUid())) {
            return r.fail("无法挑战自己!");
        }

        if (CollectionUtils.isEmpty(opponents)) {
            return r.fail("请先刷新对手列表");
        }

        if (opponents.stream()
                .noneMatch(opp -> opp.getSnapshot().uid.equalsIgnoreCase(opponentIdx))) {
            return r.fail("请先刷新对手列表");
        }

        r = this.playTimes.available(role, getFuncId());

        if (!r.ok()) {
            return r;
        }

        return r.success();
    }


    public void noticeUpdate() {
        if (isOpen()) {
            checkSeason();
        }
    }

    private boolean canChallenge() {
        return false;
    }

}
